package com.supinfo.bonjoursupinfo.database;

import java.util.ArrayList;
import java.util.Date;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.supinfo.bonjoursupinfo.activities.MainTabFragmentActivity;
import com.supinfo.bonjoursupinfo.models.News;

public class NewsBDD {
 
	public static final String TABLE_NAME = "news";
	public static final String COL_ID = "id";
	public static final int NUM_COL_ID = 0;
	public static final String COL_NAME = "name";
	public static final int NUM_COL_NAME = 1;
	public static final String COL_DESC = "description";
	public static final int NUM_COL_DESC= 2;
	public static final String COL_STARTDATE = "startDate";
	public static final int NUM_COL_STARTDATE = 3;
	public static final String COL_ENDDATE = "endDate";
	public static final int NUM_COL_ENDDATE = 4;
	public static final String COL_TEL = "tel";
	public static final int NUM_COL_TEL = 5;
	
	public SQLiteDatabase bdd;
 
	private Database Database = MainTabFragmentActivity.database; 
	
	public void open(){
		bdd = Database.getWritableDatabase();
	}
 
	public void close(){
		bdd.close();
	}
 
	public SQLiteDatabase getBDD(){
		return bdd;
	}
 
	public long insertNews(News news){
		News n = getNewsWithId(news.getId());
		if(n == null){
			return bdd.insert(TABLE_NAME, null, putValues(news));
		}else{
			return updateNews(news.getId(), news);
		}
	}
 
	public int updateNews(int id, News news){
		return bdd.update(TABLE_NAME, putValues(news), COL_ID + " = " +id, null);
	}
	
	public int removeBase(){
		return bdd.delete(TABLE_NAME, null, null);
	}
	
	public int removeNewsWithID(int id){
		return bdd.delete(TABLE_NAME, COL_ID + " = " +id, null);
	}
	
	public ArrayList<News> getAllNews() {
		Cursor c = bdd.query(TABLE_NAME, new String[] 
				{COL_ID, COL_NAME, COL_DESC, COL_STARTDATE, COL_ENDDATE, COL_TEL},
				null, null, null, null, COL_STARTDATE+" DESC");
		return cursorToNews(c);
	}
	public Cursor getCursorAllNews() {
		Cursor c = bdd.query(TABLE_NAME, new String[] 
				{COL_ID, COL_NAME, COL_DESC, COL_STARTDATE, COL_ENDDATE, COL_TEL},
				null, null, null, null, COL_STARTDATE+" ASC");
		return c;
	}
	public News getNewsWithName(String name){
		Cursor c = bdd.query(TABLE_NAME, new String[] 
				{COL_ID, COL_NAME, COL_DESC, COL_STARTDATE, COL_ENDDATE, COL_TEL},
				COL_NAME + " LIKE \"" + name +"\"", null, null, null, COL_STARTDATE +" ASC");
		return cursorToNew(c);
	}
	public News getNewsWithId(int id){
		Cursor c = bdd.query(TABLE_NAME, new String[] 
				{COL_ID, COL_NAME, COL_DESC, COL_STARTDATE, COL_ENDDATE, COL_TEL}, 
				COL_ID + " LIKE \"" + id +"\"", null, null, null, COL_STARTDATE +" ASC");
		return cursorToNew(c);
	}

	public News getNewsWithTitle(String news){
		System.out.println(news);
		Cursor c = bdd.query(TABLE_NAME, new String[] 
				{COL_ID, COL_NAME, COL_DESC, COL_STARTDATE, COL_ENDDATE, COL_TEL}, 
				COL_STARTDATE + " LIKE \"" + news +"\"", null, null, null, COL_STARTDATE +" ASC");
		News u = cursorToNew(c);
		//System.out.println(u.getClick());
		return u;
	}
	private News cursorToNew (Cursor c){
		if (c.getCount() == 0)return null;
		c.moveToFirst();
		News news= new News();
		news.setId(c.getInt(NUM_COL_ID));
		news.setName(c.getString(NUM_COL_NAME));
		news.setDescription(c.getString(NUM_COL_DESC));
		news.setStartDate(new Date(c.getLong(NUM_COL_STARTDATE)));
		news.setEndDate(new Date(c.getLong(NUM_COL_ENDDATE)));
		news.setTel(c.getString(NUM_COL_TEL));
		c.close();
		return news;
	}
	private ArrayList<News> cursorToNews(Cursor c){
		ArrayList<News> all = new ArrayList<News>();
		if(c.moveToFirst()){
			do {
				News news= new News();
				news.setId(c.getInt(NUM_COL_ID));
				news.setName(c.getString(NUM_COL_NAME));
				news.setDescription(c.getString(NUM_COL_DESC));
				news.setStartDate(new Date(c.getLong(NUM_COL_STARTDATE)));
				news.setEndDate(new Date(c.getLong(NUM_COL_ENDDATE)));
				news.setTel(c.getString(NUM_COL_TEL));
				all.add(news);
			} while (c.moveToNext());
		}
		return all;
	}

	private ContentValues putValues(News news){
		ContentValues values = new ContentValues();	
		values.put(COL_ID, news.getId());	
		values.put(COL_NAME, news.getName());
		values.put(COL_DESC, news.getDescription());
		values.put(COL_STARTDATE, news.getStartDate().getTime());
		values.put(COL_ENDDATE, news.getEndDate().getTime());
		values.put(COL_TEL, news.getTel());
		return values;
	}
	
}
